<?php

/*
 * 商品信息
*/
class Controller_Admin_Caiwu extends Controller_Template {
	/**
	 * //每页显示条数
	 * @var int
	 */
	private $page_per_count = 10;

	public function __construct($request,$response)
	{
		parent::__construct($request,$response);
		$this->template	= ('smarty:admin/caiwu/caiwu');
	}

	/**
	 * 界面展示
	 */
	public function action_index()
	{
		$page = intval($this->request->query('page')) ? intval($this->request->query('page')) : 1;
		$query = array();
		$offset = ($page -1) * $this->page_per_count;
		
		//获得已支付订单数据		
		$arr_orders=$this->get_orders($offset,$this->page_per_count);

		$this->template->set('pagination', Pagination::factory(array('total_items'=> $arr_orders['count'],'items_per_page'=>$this->page_per_count)));
		$this->template->set('pagecount',$arr_orders['count']);
		$this->template->set('orders',$arr_orders['list']);
	}

	/**
	 * 批量导出订单操作
	 */
	public function action_export()
	{		
		//获得订购单数据
		$orders_data = $this->get_orders();
		//导出订单
		$this->export_orders($orders_data['list']);
		exit;
	}
	
	/**
	 * 获得页面所需的订单数组
	 * @param int $offset 数据偏移量
	 * @param int $limit 查询数据的个数限制
	 * @return 处理后的订单数组
	 */
	private function get_orders($offset = NULL, $limit = NULL)
	{
		$query['status'] ='paid';
		$query['order_by'] = array('pay_time' => 'DESC');
		//获得订单原始信息
		$arr_result = Order::search($query,$offset,$limit);
		//加工订单数组		
		if(!empty($arr_result) && is_array($arr_result['list']) && count($arr_result['list']) > 0)
		{
			$order_data	= &$arr_result;
			foreach($arr_result['list'] as $key => $val)
			{
				//获得代理公司、销售人员、确认人员
				$arr_order_verify = Order_Verify::info($order_data['list'][$key]['order_id']);
				if(!empty($arr_order_verify) && count($arr_order_verify) > 0)
				{
					$order_data['list'][$key]['agent_corp'] = $arr_order_verify[0]['pc'];
					$order_data['list'][$key]['seller'] = $arr_order_verify[0]['sr'];
					$order_data['list'][$key]['confirm_man'] = $arr_order_verify[0]['ap'];
					$order_data['list'][$key]['update_time'] = $arr_order_verify[0]['created'];
				}
				//获取服务年限
				$order_data['list'][$key]['year'] = $order_data['list'][$key]['count'];
				
				//获得订单金额
				$order_data['list'][$key]['order_price'] = number_format((floatval($order_data['list'][$key]['count']) * floatval($order_data['list'][$key]['price'])), 2, '.', '');

				//获得商铺名称和店铺终止时间和passport
				$arr_shopinfo = Shop::info($order_data['list'][$key]['shop_id']);
				$order_data['list'][$key]['finish_time'] = $arr_shopinfo['service_time'];
				$order_data['list'][$key]['shop_name'] = $arr_shopinfo['name'];
				$order_data['list'][$key]['passport'] = $arr_shopinfo['passport'];
			}
		}
		return $order_data;
	}

	/**
	 *  批量导出订单
	 * @param unknown_type $orders_data 订单数据
	 */
	private function export_orders($orders_data)
	{
		$current_time = date('Y-m-d',time());
		$xml  = new ExcelWriterXML($current_time.'_order_data.xls');

		$sheet1  = $xml->addSheet('订单统计');

		$format1 = $xml->addStyle('big_title');
		$format1->fontSize(18);
		$format1->alignHorizontal('Center');
		$format1->alignVertical('Center');
		$format1->bgColor('#99CC33');
		$format1->fontColor('Black');

		$format2 = $xml->addStyle('deeppink');
		$format2->bgColor('DeepPink');
		$format2->fontSize(12);
		$format2->fontColor('Black');

		$format3 = $xml->addStyle('orange');
		$format3->bgColor('Orange');
		$format3->fontSize(12);
		$format3->fontColor('Black');

		$format4 = $xml->addStyle('yellow');
		$format4->bgColor('Yellow');
		$format4->fontSize(12);
		$format4->fontColor('Black');

		$format5 = $xml->addStyle('lightskyblue');
		$format5->bgColor('LightSkyBlue');
		$format5->fontSize(12);
		$format5->fontColor('Black');

		$format7 = $xml->addStyle('lightseagreen');
		$format7->bgColor('LightSeaGreen');
		$format7->fontSize(12);
		$format7->fontColor('Black');

		$format9 = $xml->addStyle('yellow');
		$format9->bgColor('Yellow');
		$format9->fontSize(12);
		$format9->fontColor('Black');

		$format11 = $xml->addStyle('lightgrey');
		$format11->fontSize(12);
		$format11->fontColor('Black');

		$sheet1->writeString(1,1,'订单统计 -- '.$current_time,$format1);
		$sheet1->cellMerge(1,1,13,0);
		$sheet1->columnWidth(1,'80');
		$sheet1->writeString(2,1,'订单号',$format2);
		$sheet1->writeString(2,2,'付款时间', $format3);
		$sheet1->writeString(2,3,'passport', $format4);
		$sheet1->writeString(2,4,'店铺ID', $format4);
		$sheet1->writeString(2,5,'店铺名称',$format4);
		$sheet1->writeString(2,6,'服务类型',$format5);
		$sheet1->writeString(2,7,'服务年限',$format5);
		$sheet1->writeString(2,8,'服务终止时间',$format3);
		$sheet1->writeString(2,9,'订单金额',$format7);
		$sheet1->writeString(2,10,'实付金额',$format7);
		$sheet1->writeString(2,11,'代理公司',$format9);
		$sheet1->writeString(2,12,'销售人员',$format9);
		$sheet1->writeString(2,13,'确认人员',$format9);
		$sheet1->writeString(2,14,'订单确认时间',$format3);

		$k	= 3;
		foreach($orders_data as $key => $val)
		{
			$sheet1->writeString($k,1,$val['order_id'],$format11);
			$sheet1->writeString($k,2,$val['pay_time'] ? date('Y-m-d H:i:s',$val['pay_time']) : '',$format11);
			$sheet1->writeString($k,3,$val['passport'],$format11);
			$sheet1->writeString($k,4,$val['shop_id'],$format11);
			$sheet1->writeString($k,5,$val['shop_name'],$format11);
			$sheet1->writeString($k,6,$val['title'],$format11);
			$sheet1->writeString($k,7,$val['year'],$format11);
			$sheet1->writeString($k,8,$val['finish_time'] ? date('Y-m-d H:i:s',$val['finish_time']) : '',$format11);
			$sheet1->writeString($k,9,$val['order_price'],$format11);
			$sheet1->writeString($k,10,$val['amount_real'],$format11);
			$sheet1->writeString($k,11,$val['agent_corp'],$format11);
			$sheet1->writeString($k,12,$val['seller'],$format11);
			$sheet1->writeString($k,13,$val['confirm_man'],$format11);
			$sheet1->writeString($k,14,$val['update_time'] ? date('Y-m-d H:i:s',$val['update_time']) : '',$format11);

			$k++;
		}

		$xml->sendHeaders();
		$xml->writeData();
	}

}
